// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 搜索功能
    const searchInput = document.getElementById('search-input');
    const searchBtn = document.getElementById('search-btn');
    const noteCards = document.querySelectorAll('.note-card');
    
    searchBtn.addEventListener('click', performSearch);
    searchInput.addEventListener('keyup', function(event) {
        if (event.key === 'Enter') {
            performSearch();
        }
    });
    
    function performSearch() {
        const searchTerm = searchInput.value.toLowerCase().trim();
        
        if (searchTerm === '') {
            // 如果搜索词为空，显示所有笔记
            noteCards.forEach(card => {
                card.style.display = 'block';
            });
            return;
        }
        
        // 遍历所有笔记卡片，检查是否包含搜索词
        noteCards.forEach(card => {
            const title = card.querySelector('h3').textContent.toLowerCase();
            const content = card.querySelector('.note-content').textContent.toLowerCase();
            
            if (title.includes(searchTerm) || content.includes(searchTerm)) {
                card.style.display = 'block';
            } else {
                card.style.display = 'none';
            }
        });
    }
    
    // 日期选择功能
    const dateSelector = document.getElementById('note-date');
    const todayNotesSection = document.getElementById('today-notes');
    const todayNotesTitle = todayNotesSection.querySelector('h2');
    
    dateSelector.addEventListener('change', function() {
        const selectedDate = new Date(this.value);
        const formattedDate = formatDate(selectedDate);
        
        // 更新标题中的日期
        todayNotesTitle.textContent = `${formattedDate} 学习笔记`;
        
        // 在实际应用中，这里应该从服务器或本地存储加载对应日期的笔记
        // 这里我们只是模拟一下
        simulateLoadingNotes();
    });
    
    function formatDate(date) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return `${year}年${month}月${day}日`;
    }
    
    function simulateLoadingNotes() {
        // 显示加载状态
        const originalHTML = todayNotesSection.innerHTML;
        todayNotesSection.innerHTML = `
            <div style="text-align: center; padding: 2rem;">
                <p>正在加载笔记...</p>
            </div>
        `;
        
        // 模拟网络请求延迟
        setTimeout(() => {
            todayNotesSection.innerHTML = originalHTML;
            // 重新绑定事件，因为元素被替换了
            bindNoteEvents();
        }, 800);
    }
    
    // 科目点击事件
    const subjects = document.querySelectorAll('.subject');
    
    subjects.forEach(subject => {
        subject.addEventListener('click', function() {
            // 过滤显示对应科目的笔记
            const subjectName = this.textContent;
            filterNotesBySubject(subjectName);
        });
    });
    
    function filterNotesBySubject(subject) {
        noteCards.forEach(card => {
            const cardSubject = card.querySelector('h3').textContent;
            
            if (subject === '所有' || cardSubject === subject) {
                card.style.display = 'block';
            } else {
                card.style.display = 'none';
            }
        });
    }
    
    // 导航菜单点击事件
    const navLinks = document.querySelectorAll('.nav-links a');
    
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            
            // 移除所有active类
            navLinks.forEach(l => l.classList.remove('active'));
            
            // 添加active类到当前点击的链接
            this.classList.add('active');
            
            // 获取目标部分ID
            const targetId = this.getAttribute('href').substring(1);
            const targetSection = document.getElementById(targetId);
            
            // 滚动到目标部分
            if (targetSection) {
                window.scrollTo({
                    top: targetSection.offsetTop - 70,
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 绑定笔记卡片事件
    function bindNoteEvents() {
        const noteCards = document.querySelectorAll('.note-card');
        
        noteCards.forEach(card => {
            card.addEventListener('dblclick', function() {
                this.classList.toggle('expanded');
            });
        });
    }
    
    // 初始化绑定事件
    bindNoteEvents();
    
    // 设置今天日期为默认值
    const today = new Date();
    const formattedToday = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
    dateSelector.value = formattedToday;
});